<?php
/**
 * Created by PhpStorm.
 * User: jack Wang
 * Date: 2021/11/17
 * Time: 15:52
 */

class Job extends Base_Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->load->model('Job_model');
        $this->load->model('Users_model');
        $this->load->service('Activity_service');
        $this->load->model('article_model');
    }

    //虚拟数据脚本
    //1 * * * * /usr/bin/php /var/www/html/iblog/index.php job/Job/job
    public function job()
    {
        $user = [
            'username' => getRandomStr(rand(3, 10)),
            'password' => encrypt('123'),
            'phone' => getRandomMobile(),
            'nickname' => getChar(rand(3, 10)),
            'sign' => getChar(rand(5, 10)),
            'email' => getRandomStr(rand(3, 10)) . '@email.com',
            'true_name' => getChar(rand(2, 4)),
            'create_time' => time(),
        ];
        //新增用户
        $this->Job_model->commonInsert('users', $user);

        //模拟用户参加活动
        //查看用户总数
        $res = $this->Users_model->commonQuery('count(id) count',[],'users');
        $count = (int)$res[0]['count'];
        for ($i = 1; $i <= $count; $i++) {
            //放入纸条
            $data1 = [
                'uid' => rand(2, 10),
                'code' => getRandomStr(4, 8),
                'type' => rand(1, 2),
                'content' => getChar(5, 10)
            ];
            curl_request("http://10.0.0.200/iblog/index.php/Activity/sendBlindBoxNote", true, $data1, false);
            //抽取纸条
            $data2 = [
                'uid' => rand(2, 10),
                'type' => rand(1, 2),
            ];
            curl_request("http://10.0.0.200/iblog/index.php/Activity/getBlindBoxNote", true, $data2, false);

            //访问文章
            $url1 = "http://10.0.0.200/iblog/index.php/article/" . rand(1, 7);
            curl_request($url1, false, '', false);
        }
    }

    /*
     * 虚拟参加掷骰子活动
     * /usr/bin/php /var/www/html/iblog/index.php job/Job/diceJob
     */
    public function diceJob()
    {
        for ($j=0;$j<rand(10,20);$j++){
            //获取随机用户
            $res = $this->Users_model->getUserCount();
            $count = $res[0]['count'];
            $userId = rand(1,$count);
            //给该用户增加骰子
            for ($i=0;$i<rand(1,10);$i++){
                $this->Activity_service->addDice($userId,getRandomStr(5));
            }
            //掷骰子
            $data= [
                'uid' => $userId
            ];
            $url = "http://10.0.0.200/iblog/index.php/Activity/rollDice";
            $res = curl_request($url,true,$data,false);
        }
    }

    /*
     * 模拟参加开启盲盒活动
     */
    public function blindJob()
    {
        for ($j=0;$j<rand(10,20);$j++){
            //获取随机用户
            $res = $this->Users_model->getUserCount();
            $count = $res[0]['count'];
            $userId = rand(1,$count);
            //给该用户增加盲盒
            $num = rand(1,10);
            $this->Activity_service->getBlindBox($userId,$num);
            //开启盲盒
            $data = [
                'uid' => $userId,
                'num' => $num
            ];
            $url = "http://10.0.0.200/iblog/index.php/Activity/openBlindBox";
            $res = curl_request($url,true,$data,false);
        }
    }


    /*
     * 模拟用户字符兑换奖励
     */
    public function charJob()
    {
        //总人数
        $count = $this->users_model->getUserCount();
        //循环次数
        $num = rand(10, $count);
        for ($i = 1; $i < $count; $i++) {
            $uid = rand(1,$count);
            $index = rand(0,4);
            $this->Activity_service->charExchange($uid,$index);
        }
    }

    /*
     * 将已经存在的文章存储到es中
     */
    public function articleToes()
    {
        $article = $this->article_model->findAll();
        $es = getEs();
        foreach ($article as $v) {
            $data = [
                'user_id' => $v['user_id'],
                'nickname' => $v['nickname'],
                'desc' => $v['desc'],
                'content' => $v['content'],
                'title' => $v['title'],
                'pageViews' => $v['pageViews'],
                'likes' => $v['likes'],
                'create_time' => $v['create_time'],
                'update_time' => date('Y-m-d H:i:s', $v['update_time'])
            ];
            $es->SaveDoc('articles', $v['id'], $data);
        }
    }

    /*
     * 根据消耗金币数获得活动抽奖卷
     */
    public function jobTicket()
    {
        $time = microtime(true);
        $this->Activity_model->useCoinGetTicket();
        echo 'execution_time===> [' . date('Y-m-d H:i:s') . '] - ' . (microtime(true) - $time) . 's Finished' . PHP_EOL;
    }

    /*
     * 根据消耗金币数获得新春活动期间消耗金币数
     */
    public function jobNewYearCoin()
    {
        $time = microtime(true);
        $this->Activity_model->newYearUsedCoin();
        echo 'execution_time===> [' . date('Y-m-d H:i:s') . '] - ' . (microtime(true) - $time) . 's Finished' . PHP_EOL;
    }
}